/**
 * 
 */
package com.easidea.tim.common.web.converter;

import java.util.Map;

import ognl.DefaultTypeConverter;

/**
 * @author Harrison
 *
 */
public class EnumTypeConverter extends DefaultTypeConverter {

	public Object convertValue(Map context, Object o, Class toClass) {
		if (o instanceof String[]) {
			return convertFromString(((String[]) o)[0], toClass);
		} else if (o instanceof String) {
			return convertFromString((String) o, toClass);
		}
		return super.convertValue(context, o, toClass);
	}

	public Object convertFromString(String value, Class toClass) {
		try {
			return Enum.valueOf(toClass, value);
		} catch (Exception e) {
			return null;
		}
	}
}